package com.wd.live.model.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**
 * 作者：马海钊
 * 时间：2023/9/4 15:20
 * 功能：
 */
abstract class BaseFragment <VM: BaseViewModel,DBD: ViewBinding>: Fragment(){
    private lateinit var _binging:DBD
    protected val binding get() = _binging
    protected lateinit var viewModel:VM
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binging = getViewBinding()
        return _binging.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViewModel()
        initData()
    }

    protected fun initViewModel() {
        val type = javaClass.genericSuperclass
        val cls = (type as ParameterizedType).actualTypeArguments[0] as Class<ViewModel>
        viewModel = ViewModelProvider(this).get<ViewModel>(cls) as VM
    }

    protected abstract fun initData()

    protected abstract fun getViewBinding(): DBD
}